/*
 * @(#)DmsSendEmailAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.DmsErrorConstant;
import com.dcivision.dms.EmailAuditTrailConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsEmailAuditTrail;
import com.dcivision.dms.bean.DmsSendEmail;
import com.dcivision.dms.core.DocumentOperationManager;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.dao.DmsEmailAuditTrailDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractMaintAction;

/**
 * <p>Class Name:       DmsClipBoardSendEmailAction.java    </p>
 * <p>Description:      The action class for DmsClipBoardSendEmai.jsp</p>
 *
 *  @author          Weison Liang
 *  @company         DCIVision Limited
 *  @creation date   16/12/2004
 *  @version         $Revision: 1.5.2.2 $
 */
public class DmsClipBoardSendEmailAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.5.2.2 $";


  /**
   *  Constructor - Creates a new instance of DmsSendEmailAction
   */
  public DmsClipBoardSendEmailAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return null;
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return null;
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {

    Connection conn = this.getConnection(request);
    SessionContainer sessionCon = this.getSessionContainer(request);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionCon, conn);
    DmsClipBoardSendEmailForm mailForm = (DmsClipBoardSendEmailForm)form;
    
    DmsSendEmail email = (DmsSendEmail)mailForm.getFormData();
    String documentID=request.getParameter("documentID");
    String [] documentIDArray=TextUtility.splitString(documentID,",");
    String version = request.getParameter("version");
    String nDocName="";
    List attchList = new ArrayList();
    for(int i=0;i<documentIDArray.length;i++){
      DmsDocument document = docRetrievalManager.getDocument(TextUtility.parseIntegerObj(documentIDArray[i]));
      String documentName=document.getDocumentName();
      nDocName=documentName+","+nDocName;
      String locationPath = docRetrievalManager.getLocationPath(document.getParentID()) +"\\" + document.getDocumentName()+ ";";
      attchList.add(locationPath);
      request.setAttribute("docName", nDocName);
    }
    if (Utility.isEmpty(mailForm.getRecipients())) {

    }
    else {
      DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionCon, conn);
      
     try{
       String state = docOperationManager.sendMultiDocumentByEmailAttachement(mailForm.getSender(), email, documentID, version);
    	 // add Email Audit Trail Record;
       DmsEmailAuditTrail dmsEmailAuditTrail = new DmsEmailAuditTrail();
       DmsEmailAuditTrailDAObject emailDAO = new DmsEmailAuditTrailDAObject(sessionCon,conn);
       dmsEmailAuditTrail.setUserid(sessionCon.getUserRecordID());
       dmsEmailAuditTrail.setOperation(EmailAuditTrailConstant.OPERATION_EMAIL_DOCUMENT);
       dmsEmailAuditTrail.setEmailType(EmailAuditTrailConstant.EMAIL_TYPE_OUTBOUND);
       dmsEmailAuditTrail.setEmailFrom(UserInfoFactory.getUserEmailAddress(sessionCon.getUserRecordID()).toString());
       dmsEmailAuditTrail.setEmailTo(mailForm.getRecipients().replaceAll(",",";"));
       dmsEmailAuditTrail.setEmailCc(mailForm.getCCRecipients().replaceAll(",",";"));
       dmsEmailAuditTrail.setEmailBcc(mailForm.getBCCRecipients().replaceAll(",",";"));
       dmsEmailAuditTrail.setEmailSubject(mailForm.getSubject());
       dmsEmailAuditTrail.setAttchSource("DMS");
       Iterator iter = attchList.iterator();
       StringBuffer sb = new StringBuffer();
       while(iter.hasNext()){
       	String attchName = (String)iter.next();
       	sb.append(attchName);
       }
       dmsEmailAuditTrail.setAttchName(sb.toString());
       dmsEmailAuditTrail.setSentDate(Utility.getCurrentTimestamp().toString());
       dmsEmailAuditTrail.setAttchCount(new Integer(attchList.size()));
       dmsEmailAuditTrail.setRecordStatus(GlobalConstant.RECORD_STATUS_ACTIVE);
  	   dmsEmailAuditTrail.setCreatorID(sessionCon.getUserRecordID());
  	   dmsEmailAuditTrail.setCreateDate(Utility.getCurrentTimestamp());
  	   dmsEmailAuditTrail.setUpdateCount(new Integer(0));
  	   dmsEmailAuditTrail.setUpdaterID(sessionCon.getUserRecordID());
  	   dmsEmailAuditTrail.setUpdateDate(Utility.getCurrentTimestamp());
       dmsEmailAuditTrail = (DmsEmailAuditTrail)emailDAO.insertObject(dmsEmailAuditTrail);
       conn.commit();

       request.setAttribute("state", state);
       docOperationManager.release();
     }catch(ApplicationException appe){
       this.addError(request, DmsErrorConstant.ERROR_DMS_SEND_EMAIL);
     }catch(Exception e){
       log.error(e, e);
     }
    }
    try {
      sessionCon=null;
      docRetrievalManager.release();
    } catch (Exception e) {
      log.error(e, e);
    } finally {
      conn = null;
    }
    return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
  }
}